package com.king.hrmsdev.controller;

import com.alibaba.fastjson.JSONObject;
import com.king.hrmsdev.entity.Aleave;
import com.king.hrmsdev.entity.Echeck;
import com.king.hrmsdev.pojo.Valeaveinfo;
import com.king.hrmsdev.pojo.Vecheckinfo;
import com.king.hrmsdev.service.AttendanceService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:SnakeKing
 * @Date: 2019-09-22
 * @Description: com.king.hrmsdev.controller
 * @version:1.0
 */
@RestController
public class AttendanceController {

    @Resource
    private AttendanceService attendanceservice;

    //查询所有假条
    @RequestMapping(value = "/Findallaleave", method = RequestMethod.POST)
    public JSONObject Findallaleave() {
        List<Valeaveinfo> FindallList = attendanceservice.Findallaleave();
        JSONObject result = new JSONObject();
        result.put("msg", "ok");
        result.put("method", "Findallaleave");
        result.put("allaleaveinfolist", FindallList);
        return result;
    }

    //同意假条
    @RequestMapping(value = "/Approvalaleave", method = RequestMethod.POST)
    public int Approvalaleave(@RequestParam("aleave_id") int aleave_id) {
        int flag = attendanceservice.Approvalaleave(aleave_id);
        return flag;
    }

    //拒绝假条
    @RequestMapping(value = "/Refusealeave", method = RequestMethod.POST)
    public int Refusealeave(@RequestParam("aleave_id") int aleave_id) {
        int flag = attendanceservice.Refusealeave(aleave_id);
        return flag;
    }

    //添加假条
    @RequestMapping(value = "/Insertaleave", method = RequestMethod.POST)
    public int Insertaleave(@RequestParam("job_id") int job_id,
                            @RequestParam("btime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date btime,
                            @RequestParam("etime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date etime,
                            @RequestParam(value = "reason", required = false) String reason,
                            @RequestParam("examine") int examine) {
        Aleave aleave = new Aleave();
        aleave.setJob_id(job_id);
        aleave.setBtime(btime);
        aleave.setEtime(etime);
        if (reason != null) {
            aleave.setReason(reason);
        }
        aleave.setExamine(examine);
        int flag = attendanceservice.Insertaleave(aleave);
        return flag;
    }

    //假条模糊查询
    @RequestMapping(value = "/AleaveFuzzyreward", method = RequestMethod.POST)
    public List<Valeaveinfo> AleaveFuzzyreward(@RequestParam(value = "job_id", required = false, defaultValue = "") Integer job_id,
                                               @RequestParam(value = "aleave_id", required = false, defaultValue = "") Integer aleave_id,
                                               @RequestParam(value = "ename", required = false) String ename,
                                               @RequestParam(value = "beginDate", required = false) String btime,
                                               @RequestParam(value = "endDate", required = false) String etime) {

        Map map = setmapbycustom(job_id,null,ename,null,null,aleave_id,btime,etime);
        List list = attendanceservice.AleaveFuzzyreward(map);
        return list;
    }

    //查询所有考勤
    @RequestMapping(value = "/Findallecheck", method = RequestMethod.POST)
    public JSONObject Findallecheck() {
        List<Vecheckinfo> vecheckinfoList = attendanceservice.Findallecheck();
        JSONObject result = new JSONObject();
        result.put("msg", "ok");
        result.put("method", "Findallecheck");
        result.put("allecheckinfoList", vecheckinfoList);
        return result;
    }

    //添加考勤
    @RequestMapping(value = "/Insertecheck", method = RequestMethod.POST)
    public int Insertecheck(@RequestParam(value = "echeck_id", required = false, defaultValue = "") Integer echeck_id,
                            @RequestParam("job_id") Integer job_id,
                            @RequestParam(value = "opentime", required = false) String opentime,
                            @RequestParam(value = "closetime", required = false) String closetime,
                            @RequestParam("state") Integer state) {
        Echeck echeck = new Echeck();
        echeck.setJob_id(job_id);
        echeck.setOpentime(opentime);
        echeck.setClosetime(closetime);
        echeck.setState(state);
        int flag = attendanceservice.Insertecheck(echeck);
        return flag;
    }


    //修改考勤
    @RequestMapping(value = "/Updateecheck", method = RequestMethod.POST)
    public int Updateecheck(@RequestParam("echeck_id") Integer echeck_id,
                            @RequestParam("job_id") Integer job_id,
                            @RequestParam("opentime") String opentime,
                            @RequestParam(value = "closetime", required = false) String closetime,
                            @RequestParam("state") Integer state) {
        Echeck echeck = new Echeck();
        echeck.setEcheck_id(echeck_id);
        echeck.setJob_id(job_id);
        echeck.setOpentime(opentime);
        echeck.setClosetime(closetime);
        echeck.setState(state);
        int flag = attendanceservice.Updateecheck(echeck);
        return flag;
    }

    //删除考勤
    @RequestMapping(value = "/Deleteecheck", method = RequestMethod.POST)
    public int Deleteecheck(@RequestParam("echeck_id") int echeck_id) {
        int flag = attendanceservice.Deleteecheck(echeck_id);
        return flag;
    }

    //模糊查询
    @RequestMapping(value = "/EcheckFuzzyreward", method = RequestMethod.POST)
    public List<Vecheckinfo> EcheckFuzzyreward(@RequestParam(value = "job_id", required = false, defaultValue = "") Integer job_id,
                                               @RequestParam(value = "echeck_id", required = false, defaultValue = "") Integer echeck_id,
                                               @RequestParam(value = "ename", required = false) String ename,
                                               @RequestParam(value = "opentime", required = false) String opentime,
                                               @RequestParam(value = "closetime", required = false) String closetime) {


        Map map = setmapbycustom(job_id,echeck_id,ename,opentime,closetime,null,null,null);
        List list = attendanceservice.EcheckFuzzyreward(map);
        return list;
    }

    public Map setmapbycustom(Integer job_id,Integer echeck_id,String ename,String opentime,String closetime, Integer aleave_id,
                              String btime,String etime){
        Map map = new HashMap();
        if (opentime == "") {
            opentime = null;
        }
        if (closetime == "") {
            closetime = null;
        }
        map.put("job_id", job_id);
        map.put("echeck_id", echeck_id);
        map.put("ename", ename);
        map.put("opentime", opentime);
        map.put("closetime", closetime);
        map.put("aleave_id", aleave_id);
        map.put("btime", btime);
        map.put("etime", etime);

        return map;
    }

}
